import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { IdentityRoleDto } from '../proxy/identity';

@Component({
  selector: 'app-ref-role-detail',
  templateUrl: './ref-role-detail.component.html',
})
export class RefRoleDetailComponent implements OnInit {
  validateForm: FormGroup;
  @Input() model: IdentityRoleDto;
  constructor(public fb: FormBuilder, public modalRef: NzModalRef) {}
  ngOnInit(): void {
    this.validateForm = this.fb.group({
      name: [this.model?.name, [Validators.required]],
      isPublic: [this.model?.isPublic ?? true],
      isDefault: [this.model?.isDefault ?? false],
    });
  }

  cancelForm() {
    this.modalRef.close(null);
  }

  public submitForm() {
    if (!this.validateForm.valid) return;

    this.modalRef.close({
      ...this.model,
      name: this.validateForm.value.name,
      isPublic: !!this.validateForm.value.isPublic,
      isDefault: !!this.validateForm.value.isDefault,
    });
  }
}
